﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Controllers.KhoaService;
using eTMS.Controllers.LopService;
using eTMS.Controllers.NganhHocService;
using eTMS.Forms;

namespace eTMS.UserControls
{
    public partial class TreeKhoaNganhLop : eTMS.Controls.UserControlBase
    {
        public TreeKhoaNganhLop()
        {
            InitializeComponent();
        }


        public event EventHandler OnChangeTreeSelected;
        private void TreeKhoaNganh_Load(object sender, EventArgs e)
        {
        }
        private bool _IsShowNganh = true;
        public bool IsShowNganh
        {
            set
            {
                _IsShowNganh = value;
            }
        }
        private bool _IsShowLop = true;
        public bool IsShowLop
        {
            set
            {
                _IsShowLop = value;
            }
        }
        private bool _IsShowSoSVToiDaLop = false;
        public bool IsShowSoSVToiDaLop
        {
            set
            {
                _IsShowSoSVToiDaLop = value;
            }
        }
        private int _KhoaHoc_ID = 0;
        public int KhoaHoc_ID
        {
            set
            {
                _KhoaHoc_ID = value;
            }
        }
        LopController lopCtrl;
        public void UpdateToScreen()
        {
            if (DesignMode) return;
            treeView1.Nodes.Clear();
            TreeNode root = new TreeNode();
            root.Text = CommonController.HeDaoTao.TenHeDaoTao;
            root.ImageIndex = 0;
            root.SelectedImageIndex = 0;
            KhoaController.LoadAllKhoa(false);
            NganhHocController.LoadAllNganhHoc(false);
            treeView1.Nodes.Add(root);
            if (_KhoaHoc_ID > 0)
            {
                if (lopCtrl == null)
                    lopCtrl = new LopController();
                lopCtrl.LoadAllLopWithSoSinhVienGiaoVienByNganhHoc_IDKhoaHoc_IDKhoa_ID(0, _KhoaHoc_ID, 0);
            }
            foreach (KhoaEntity khoaEntity in KhoaController.ListKhoaStatic)
            {
                TreeNode node = new TreeNode();
                node.Text = KhoaStringMapping.Khoa + ":" + khoaEntity.TenKhoa;
                node.Tag = khoaEntity;
                root.Nodes.Add(node);
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;
                if (_IsShowNganh)
                {
                    foreach (NganhHocEntity nganhHocEntity in NganhHocController.FindNganhHocByKhoa_ID(khoaEntity.Khoa_ID))
                    {
                        TreeNode childNode = new TreeNode();
                        childNode.Text = nganhHocEntity.MaNganhHoc + ":" + nganhHocEntity.TenNganhHoc;
                        childNode.Tag = nganhHocEntity;
                        node.Nodes.Add(childNode);
                        childNode.ImageIndex = 2;
                        childNode.SelectedImageIndex = 2;
                        if (_KhoaHoc_ID > 0)
                        {
                            if (_IsShowLop)
                            {
                                foreach (DataRow rowLop in lopCtrl.Table.Select("NganhHoc_ID=" + nganhHocEntity.NganhHoc_ID.ToString()))
                                {
                                    LopEntity lopEntity = lopCtrl.MapDataRowToObject(rowLop);
                                    TreeNode lopNode = new TreeNode();
                                    lopNode.Text = lopEntity.TenLop + "(" + rowLop["SoSinhVien"].ToString() + (_IsShowSoSVToiDaLop ? "/" + lopEntity.SoSinhVienToiDa.ToString() : "") + ")";
                                    lopNode.Tag = lopEntity;
                                    childNode.Nodes.Add(lopNode);
                                    lopNode.ImageIndex = 3;
                                    lopNode.SelectedImageIndex = 3;
                                }
                            }
                        }
                    }
                }
            }
            root.Expand();
        }

        public int SelectedKhoa_ID
        {
            get
            {
                if (treeView1.SelectedNode == null) return -1;
                if (treeView1.SelectedNode.Tag is KhoaEntity)
                {
                    return ((KhoaEntity)treeView1.SelectedNode.Tag).Khoa_ID;
                }
                return -1;
            }
        }
        public KhoaEntity SelectedKhoa
        {
            get
            {
                if (treeView1.SelectedNode == null) return null;
                if (treeView1.SelectedNode.Tag is KhoaEntity)
                {
                    return (KhoaEntity)treeView1.SelectedNode.Tag;
                }
                return null;
            }
        }
        public int SelectedNganhHoc_ID
        {
            get
            {
                if (treeView1.SelectedNode == null) return -1;
                if (treeView1.SelectedNode.Tag is NganhHocEntity)
                {
                    return ((NganhHocEntity)treeView1.SelectedNode.Tag).NganhHoc_ID;
                }
                return -1;
            }
        }
        public NganhHocEntity SelectedNganhHoc
        {
            get
            {
                if (treeView1.SelectedNode == null) return null;
                if (treeView1.SelectedNode.Tag is NganhHocEntity)
                {
                    return (NganhHocEntity)treeView1.SelectedNode.Tag;
                }
                return null;
            }
        }
        public List<LopEntity> GetSelectedListLop()
        {
            List<LopEntity> lsLop = new List<LopEntity>();
            List<TreeNode> lsNodes = new List<TreeNode>();
            foreach (TreeNode node in treeView1.SelectedNodes)
            {
                AddLopToList(node, lsNodes);
            }
            foreach (TreeNode node in lsNodes)
                lsLop.Add(((LopEntity)node.Tag));
            return lsLop;
        }
        public List<DataRow> GetSelectedListDataRowLop()
        {
            List<string> lsID = new List<string>();
            foreach (LopEntity eLop in GetSelectedListLop())
            {
                lsID.Add(eLop.Lop_ID.ToString());
            }
            List<DataRow> ls = new List<DataRow>();
            if (lsID.Count > 0)
                ls.AddRange(lopCtrl.Table.Select("Lop_ID IN(" + string.Join(",", lsID.ToArray()) + ")"));
            return ls;
        }
        public void UpdateLopNodeText()
        {
            TreeNode node = treeView1.SelectedNode;
            LopEntity lopEntity = (LopEntity)node.Tag;
            string st = node.Text;
            int i = st.IndexOf("(");
            if (i > 0)
                st = st.Substring(i);
            node.Text = lopEntity.TenLop + st;
        }
        public void RemoveSelectedListLop()
        {
            List<TreeNode> lsNodes = new List<TreeNode>();
            foreach (TreeNode node in treeView1.SelectedNodes)
            {
                AddLopToList(node, lsNodes);
            }
            foreach (TreeNode nd in lsNodes)
                nd.Remove();
        }
        private void AddLopToList(TreeNode node, List<TreeNode> lsLop)
        {
            if (node == null) return;
            if (node.Tag is LopEntity)
            {
                if (!lsLop.Contains(node))
                    lsLop.Add(node);
            }
            foreach (TreeNode sNode in node.Nodes)
            {
                AddLopToList(sNode, lsLop);
            }
        }
        public int SelectedLop_ID
        {
            get
            {
                if (treeView1.SelectedNode == null) return -1;
                if (treeView1.SelectedNode.Tag is LopEntity)
                {
                    return ((LopEntity)treeView1.SelectedNode.Tag).Lop_ID;
                }
                return -1;
            }
            set
            {
                if (treeView1.Nodes.Count > 0)
                {
                    SetSelectedLop(value, treeView1.Nodes[0]);
                }
            }
        }
        private bool SetSelectedLop(int Lop_ID, TreeNode node)
        {
            if (node.Tag is LopEntity)
            {
                if (((LopEntity)node.Tag).Lop_ID == Lop_ID)
                {
                    treeView1.SelectedNode = node;
                    node.EnsureVisible();
                    return true;
                }
            }
            foreach (TreeNode sNode in node.Nodes)
            {
                if (SetSelectedLop(Lop_ID, sNode))
                {
                    node.Expand();
                    return true;
                }
            }
            return false;
        }
        public LopEntity SelectedLop
        {
            get
            {
                if (treeView1.SelectedNode == null) return null;
                if (treeView1.SelectedNode.Tag is LopEntity)
                {
                    return (LopEntity)treeView1.SelectedNode.Tag;
                }
                return null;
            }
        }

        public int CurrentNganhHoc_ID
        {
            get
            {
                if (treeView1.SelectedNode == null) return -1;
                if (treeView1.SelectedNode.Tag is LopEntity)
                {
                    return ((LopEntity)treeView1.SelectedNode.Tag).NganhHoc_ID;
                }
                if (treeView1.SelectedNode.Tag is NganhHocEntity)
                {
                    return ((NganhHocEntity)treeView1.SelectedNode.Tag).NganhHoc_ID;
                }
                return -1;
            }
        }


        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (OnChangeTreeSelected != null)
            {
                OnChangeTreeSelected(sender, new EventArgs());
            }
        }
    }
}
